package com.snj.twelve;

import java.util.ArrayList;
import java.util.List;

// 车辆类
class Vehicle {
    int capacity;
    int load;

    public Vehicle(int capacity) {
        this.capacity = capacity;
        this.load = 0;
    }

    // 装载货物
    public boolean load(int amount) {
        if (load + amount <= capacity) {
            load += amount;
            return true;
        }
        return false;
    }

    // 判断是否满载
    public boolean isFull() {
        return load == capacity;
    }
}

// 仓库类
class Warehouse {
    List<Vehicle> vehicles;
    int totalSupply;

    public Warehouse(int totalSupply) {
        this.totalSupply = totalSupply;
        this.vehicles = new ArrayList<>();
    }

    // 添加车辆
    public void addVehicle(int capacity) {
        vehicles.add(new Vehicle(capacity));
    }

    // 分配物资
    public List<Vehicle> distribute() {
        List<Vehicle> dispatchedVehicles = new ArrayList<>();
        for (Vehicle vehicle : vehicles) {
            while (totalSupply > 0 && !vehicle.isFull()) {
                int amountToLoad = Math.min(totalSupply, vehicle.capacity - vehicle.load);
                vehicle.load(amountToLoad);
                totalSupply -= amountToLoad;
                if (vehicle.isFull()) {
                    dispatchedVehicles.add(vehicle);
                }
            }
        }
        return dispatchedVehicles;
    }
}

// 总仓类，继承自仓库类
class MainWarehouse extends Warehouse {
    public MainWarehouse(int totalSupply) {
        super(totalSupply);
    }

    // 总仓分配物资
    @Override
    public List<Vehicle> distribute() {
        List<Vehicle> dispatchedVehicles = new ArrayList<>();
        while (totalSupply >= 3.5) {
            Vehicle vehicle = new Vehicle(5);
            int amountToLoad = (int) Math.min(totalSupply, 4.4);
            vehicle.load(amountToLoad);
            totalSupply -= amountToLoad;
            if (amountToLoad >= 3.5) {
                dispatchedVehicles.add(vehicle);
            }
        }
        return dispatchedVehicles;
    }
}

// 分仓类，继承自仓库类
class SubWarehouse extends Warehouse {
    public SubWarehouse(int totalSupply) {
        super(totalSupply);
    }
}

public class DeliveryAllocation {
    public static void main(String[] args) {
        // 总仓物资总量
        int mainWarehouseSupply = 20;
        MainWarehouse mainWarehouse = new MainWarehouse(mainWarehouseSupply);

        // 分仓物资需求
        int[] subWarehouseSupplies = {3, 4, 5, 6};
        List<SubWarehouse> subWarehouses = new ArrayList<>();
        for (int supply : subWarehouseSupplies) {
            SubWarehouse subWarehouse = new SubWarehouse(supply);
            // 分仓添加车辆
            subWarehouse.addVehicle(2);
            subWarehouse.addVehicle(3);
            subWarehouse.addVehicle(5);
            subWarehouses.add(subWarehouse);
        }

        // 总仓分配物资
        List<Vehicle> mainDispatchedVehicles = mainWarehouse.distribute();
        System.out.println("总仓分配的车辆：");
        for (Vehicle vehicle : mainDispatchedVehicles) {
            System.out.println("承载 " + vehicle.capacity + "T 的车辆，装载 " + vehicle.load + "T 物资");
        }

        // 分仓分配物资
        for (int i = 0; i < subWarehouses.size(); i++) {
            SubWarehouse subWarehouse = subWarehouses.get(i);
            List<Vehicle> subDispatchedVehicles = subWarehouse.distribute();
            System.out.println("分仓 " + (i + 1) + " 分配的车辆：");
            for (Vehicle vehicle : subDispatchedVehicles) {
                System.out.println("承载 " + vehicle.capacity + "T 的车辆，装载 " + vehicle.load + "T 物资");
            }
        }
    }
}